package cn.rulian.car.outbill.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rulian.base.auth.ann.Access;
import cn.rulian.base.auth.bo.User;
import cn.rulian.base.common.dto.Message;
import cn.rulian.base.common.dto.UserInfo;
import cn.rulian.base.common.vo.Result;
import cn.rulian.car.car.bo.Car;
import cn.rulian.car.car.service.CarService;
import cn.rulian.car.outbill.bo.OutBill;
import cn.rulian.car.outbill.bo.OutBillFlow;
import cn.rulian.car.outbill.dao.OutBillStateDao;
import cn.rulian.car.outbill.dto.Rate;
import cn.rulian.car.outbill.service.OutBillFlowService;
import cn.rulian.car.outbill.service.OutBillService;
import cn.rulian.car.outbill.vo.OutBillVo;

/**
 * 车辆外出登记表
 */

@Access(authCode="outBill")
@Controller
@RequestMapping("/car/outbill/outBill")
public class OutBillController {
	
	@Autowired
	private OutBillService service;
	
	@Autowired
	private CarService carService;

	@Autowired
	private OutBillStateDao outBillStateDao;
	
	@Autowired
	private OutBillFlowService outBillFlowService; 
	
	//private final Logger log = LoggerFactory.getLogger(getClass());
	
	private final String path="car/outbill/outbill_"; //页面基本路径

	/**
	 * 页面基本路径
	 */
	private String getPath()
	{
		return path;
	}
	
	
	/**
	 * 列表页面
	 */
	@RequestMapping("/listPage")
	public String listPage() {
		return this.getPath()+"list";
	}

	/**
	 * 查询
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Result listData(OutBillVo vo) {
		vo.setDataSource(OutBill.DATASOURCE_ADD);
		return service.query(vo);
	}

	/**
	 * 增加页面
	 */
	@RequestMapping("/addPage")
	public String addPage(Model model) {
		Rate rate = service.getRate(); //费率
		model.addAttribute("rate",rate);
		return this.getPath()+"add";
	}

	/**
	 * 增加
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Message add(OutBillVo vo) {
		Message rm = new Message();
		OutBill bo = vo.getObj();
		bo.setAddTime(new Date());
		
		bo.setState(outBillStateDao.findFirstByCode("08")); //状态：完成
		bo.setDataSource(OutBill.DATASOURCE_ADD);//数据来源（申请）
		bo.setOpeTimeNear(new Date()); //最近操作时间
		
		//更新车辆信息的 公里数和车辆状况
		Car car = carService.findOne(bo.getCar().getId());
		car.setEndNum(bo.getEndNum());
		car.setCarStatus(bo.getCarStatus());
		
		try {
			carService.save(car);
			service.save(bo);
			
			//加测试数据
			/*for(int i=0;i<50;i++)
			{
				OutBill one = new OutBill();
				xx.copyObjectField(bo, one);
				service.save(one);
			}*/
			
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 修改页面
	 */
	@RequestMapping("/updatePage")
	public String updatePage(Model model, Long id) {
		Rate rate = service.getRate(); //费率
		model.addAttribute("rate",rate);
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"update";
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Message update(HttpSession session,OutBillVo vo,Long id) {
		Message rm = new Message();
		try {
			OutBill dto = vo.getObj();
			OutBill bo = service.findOne(id);
			
			bo.setCar(dto.getCar());
			bo.setDriver(dto.getDriver());
			bo.setCarStatus(dto.getCarStatus());
			bo.setOrgan(dto.getOrgan());
			bo.setUser(dto.getUser());
			bo.setApprover(dto.getApprover());
			bo.setDestination(dto.getDestination());
			bo.setBeginTime(dto.getBeginTime());
			bo.setEndTime(dto.getEndTime());
			bo.setUseTime(dto.getUseTime());
			bo.setBeginNum(dto.getBeginNum());
			bo.setEndNum(dto.getEndNum());
			bo.setUseNum(dto.getUseNum());
			bo.setWaitTime(dto.getWaitTime());
			bo.setFuelNum(dto.getFuelNum());
			bo.setOtherFee(dto.getOtherFee());
			bo.setAllFee(dto.getAllFee());
			bo.setRemark(dto.getRemark());
			bo.setOpeTimeNear(new Date());
			bo.setState(outBillStateDao.findFirstByCode("08")); //状态：完成
			
			service.save(bo);
			
			if(OutBill.DATASOURCE_APPLE.equals(bo.getDataSource()))
			{//对于申请的，增加流转记录
				UserInfo userInfo = UserInfo.getUserInfo(session); 
				User user = userInfo.getUser();
				
				OutBillFlow flow = new OutBillFlow();
				flow.setOutBill(bo);
				flow.setOpe("回录");
				flow.setOpeUserName(user.getName());
				flow.setOpeTime(new Date());
				
				outBillFlowService.save(flow);
			}
			
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Message delete(Long ids[]) {
		Message rm = new Message();
		try {
			for(Long id:ids)
			{
				service.delete(id);
			}
			
			rm.setMsg("删除成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("删除失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 详情页面
	 */
	@RequestMapping("/detailPage")
	public String detailPage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"detail";
	}

	
}
